package com.mobyd.mweb.common.util.paginate;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Paginate {
	
	private static final Logger log = LoggerFactory.getLogger(Paginate.class);
	
	// 페이지사이즈
	private int pageSize;
	
	// 현재 페이지
	private int currentPage;
	
	// 총 페이지수
	private int totalPages;
	
	// 현재페이지 시작번호
	private int startPageNum;
	
	// 현재페이지 종료번호
	private int endPageNum;
	
	// 더보기 유무
	private boolean hasNext;
	
	private int totalRecords;
	
	public void initialize(Map<String, Object> param) {
		String currentPageStr = (String)param.get("currentPage");
		currentPage = StringUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		if(currentPage<1) {
			currentPage = 1;
		}
		
		startPageNum = (currentPage - 1) * pageSize + 1;
		endPageNum = currentPage * pageSize;
		
		param.put("currentPage", currentPage);
		param.put("pageSize", pageSize);
		param.put("startPageNum", startPageNum);
		param.put("endPageNum", endPageNum);
	}
	
	
	public void setTotalRecords(Map<String, Object> param, int totalRecords) {
		this.totalRecords = totalRecords;
		totalPages = (totalRecords - 1) / pageSize + 1;
		if(currentPage < totalPages) {
			hasNext = true;
		} else {
			hasNext = false;
		}
		
		param.put("totalRecords", totalRecords);
		param.put("totalPages", totalPages);
		param.put("hasNext", hasNext);
	}
	
	
	public void initialize(Map<String, Object> param, int totalRecords) {
		String currentPageStr = (String)param.get("currentPage");
		currentPage = StringUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		if(currentPage<1) {
			currentPage = 1;
		}
		
		this.totalRecords = totalRecords;
		totalPages = (totalRecords - 1) / pageSize + 1;
		
		startPageNum = (currentPage - 1) * pageSize + 1;
		endPageNum = currentPage * pageSize;
		
		if(currentPage < totalPages) {
			hasNext = true;
		} else {
			hasNext = false;
		}
		
		param.put("currentPage", currentPage);
		param.put("pageSize", pageSize);
		param.put("totalRecords", totalRecords);
		param.put("totalPages", totalPages);
		param.put("startPageNum", startPageNum);
		param.put("endPageNum", endPageNum);
		param.put("hasNext", hasNext);
	}

	
	public void initNavi(Map<String, Object> param, int totalRecords) {
		String currentPageStr = (String)param.get("currentPage");
		currentPage = StringUtils.isEmpty(currentPageStr) ? 1 : Integer.parseInt(currentPageStr);
		if(currentPage<1) {
			currentPage = 1;
		}
		
		totalPages = (totalRecords - 1) / pageSize + 1;
		
		startPageNum = (currentPage - 1) * pageSize + 1;
		endPageNum = currentPage * pageSize;
		
		if(currentPage < totalPages) {
			hasNext = true;
		} else {
			hasNext = false;
		}
		
		param.put("currentPage", currentPage);
		param.put("pageSize", pageSize);
		param.put("totalRecords", totalRecords);
		param.put("totalPages", totalPages);
		param.put("startPageNum", startPageNum);
		param.put("endPageNum", endPageNum);
		param.put("hasNext", hasNext);
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
	
	public int getTotalRecords() {
		return totalRecords;
	}
	
}
